function varargout = analyzeWormConsole(varargin)
% ANALYZEWORMCONSOLE MATLAB code for analyzeWormConsole.fig
%      ANALYZEWORMCONSOLE, by itself, creates a new ANALYZEWORMCONSOLE or raises the existing
%      singleton*.
%
%      H = ANALYZEWORMCONSOLE returns the handle to a new ANALYZEWORMCONSOLE or the handle to
%      the existing singleton*.
%
%      ANALYZEWORMCONSOLE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ANALYZEWORMCONSOLE.M with the given input arguments.
%
%      ANALYZEWORMCONSOLE('Property','Value',...) creates a new ANALYZEWORMCONSOLE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before analyzeWormConsole_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to analyzeWormConsole_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help analyzeWormConsole

% Last Modified by GUIDE v2.5 26-Feb-2014 12:15:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @analyzeWormConsole_OpeningFcn, ...
                   'gui_OutputFcn',  @analyzeWormConsole_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before analyzeWormConsole is made visible.
function analyzeWormConsole_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to analyzeWormConsole (see VARARGIN)

% Choose default command line output for analyzeWormConsole
handles.output = hObject;

% UIWAIT makes analyzeWormConsole wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%Initialize a timer object to display image frames as movie
handles.timer = timer(...
    'ExecutionMode', 'fixedRate', ...       % Run timer repeatedly
    'Period', 1, ...                        % Initial period is 1 sec.
    'TimerFcn', {@execUpdateDisplay,hObject}); % Specify callback function

% Create a surface plot of peaks data. Store handle to it.
handles.dataList = cell2mat(varargin(1));
handles.theSuppData = cell2mat(varargin(2));
handles.nFrames = length(handles.theSuppData);
[handles.rows, handles.cols]  = size(handles.theSuppData(1).GrayScaleImage);
handles.curFrame = 1;  
handles.trail = genTrail(handles.theSuppData);
handles.trajectory = genTrajectory(handles.dataList);
guidata(hObject, handles);
% Update handles structure

showDataTable(handles.uitable1, handles.dataList);
updateDisplay(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = analyzeWormConsole_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes on button press in pushbutton_play.
function pushbutton_play_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_play (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    if handles.curFrame == handles.nFrames
           handles.curFrame = 1;
         
    end
    guidata(hObject, handles);
    if strcmp(handles.timer.Running,'off')
       start(handles.timer);
    end
    guidata(hObject, handles);
    
 
% --- Executes on button press in pushbutton_stop.
function pushbutton_stop_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

stop(handles.timer);
guidata(hObject, handles);

% --- Executes on button press in pushbutton_stepForward.
function pushbutton_stepForward_Callback(hObject, ~, handles)
% hObject    handle to pushbutton_stepForward (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
if strcmp(handles.timer.Running,'on')
            stop(handles.timer);
end
handles.curFrame = handles.curFrame + 1;
guidata(hObject, handles);
 updateDisplay(hObject, handles);

% --- Executes on button press in pushbutton_stepBack.
function pushbutton_stepBack_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_stepBack (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if strcmp(handles.timer.Running, 'on')
    stop(handles.timer);
end
if handles.curFrame ~= 1
   handles.curFrame = handles.curFrame - 1;
   guidata(hObject, handles);
end
updateDisplay(hObject, handles);
       

    
        

function edi_iFrame_Callback(hObject, eventdata, handles)
% hObject    handle to edi_iFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edi_iFrame as text
%        str2double(get(hObject,'String')) returns contents of edi_iFrame as a double


% --- Executes during object creation, after setting all properties.


function edit_nFrames_Callback(hObject, eventdata, handles)
% hObject    handle to edit_nFrames (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_nFrames as text
%        str2double(get(hObject,'String')) returns contents of edit_nFrames as a double


% --- Executes during object creation, after setting all properties.
function edit_nFrames_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_nFrames (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_gotoFrame_Callback(hObject, eventdata, handles)
% hObject    handle to edit_gotoFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit_gotoFrame as text
%        str2double(get(hObject,'String')) returns contents of edit_gotoFrame as a double


% --- Executes during object creation, after setting all properties.
function edit_gotoFrame_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_gotoFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_gotoFrame.
function pushbutton_gotoFrame_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_gotoFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

oldFrame = handles.curFrame;
strCurFrame = get(handles.edit_gotoFrame, 'String');
newFrame = str2num(strCurFrame);

handles.curFrame = newFrame;
guidata(hObject, handles);
updateDisplay(hObject, handles);





% --- Executes during object creation, after setting all properties.
function edit_iFrame_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_iFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function edit_iFrame_Callback(hObject, eventdata, handles)
% hObject    handle to edit_iFrame (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



% Hints: get(hObject,'String') returns contents of edit_iFrame as text
%        str2double(get(hObject,'String')) returns contents of edit_iFrame as a double
function updateDisplay(hObject, handles)
       
       %Display the grayscale image uncropped
       showGrayScale(handles.axesVideo, handles.theSuppData, handles.curFrame);
              
       %Display the trajectory
       plot(handles.axesTrajectory, handles.trajectory(1:handles.curFrame,1), handles.trajectory(1:handles.curFrame,2),...
           '--rs','LineWidth',2,...
           'MarkerEdgeColor','k',...
           'MarkerFaceColor','g',...
           'MarkerSize',10); 
       
      
        %Display the gray image cropped with overlays 
         showSegAndSkel(handles.axesSegment, handles.dataList,...
                         handles.theSuppData, handles.curFrame);    
          
       %Display the track based on the skeleton
       showTrail(handles.axesTrack, handles.trail,  handles.curFrame, [handles.rows, handles.cols]);
       
       
       %Display the current frame number
        set(handles.edit_iFrame,'String',num2str(handles.curFrame));
        set(handles.edit_nFrames,'String',num2str(handles.nFrames) );
        drawnow;
        
        %Update the handles 
        guidata(hObject, handles);

function execUpdateDisplay(hObject,eventdata,hfigure)
    handles = guidata(hfigure);
     if handles.curFrame ~= handles.nFrames
            handles.curFrame = handles.curFrame + 1;
            guidata(hfigure, handles);
            updateDisplay(hfigure, handles);
            
       else
            stop (handles.timer);
            
     end
       guidata(hfigure,handles);
  
